/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thitracnghiem;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TienNT
 */
public class DeThi {

    int lanThi;
    int mSoCau;
    String mMaMonThi;
    ArrayList<CauHoi> mListCauHoi = new ArrayList<CauHoi>();

    public int getLanThi() {
        return lanThi;
    }

    public void setLanThi(int lanThi) {
        this.lanThi = lanThi;
    }

    public String getTenMonThi() {
        String tenMon = "";
        try {
            openDatabase();
            String query = "select tenmonthi from MonThi where mamonthi='" + mMaMonThi + "'";
            ResultSet rs = stm.executeQuery(query);

            if (rs.next()) {
                tenMon = rs.getString("tenmonthi");
            }
            closeDatabase();
        } catch (SQLException ex) {
            Logger.getLogger(DeThi.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tenMon;
    }

    public DeThi() {
    }
    static Connection conn;
    static Statement stm;

    public static void openDatabase() throws SQLException {
        conn = DriverManager.getConnection("jdbc:odbc:ttn");
        stm = conn.createStatement();
    }

    public String getDsDapAn() {
        StringBuilder sb = new StringBuilder();
        for (CauHoi ch : mListCauHoi) {
            sb.append(ch.getDapAn() + ";");
        }
        sb.deleteCharAt(sb.length() - 1);
        return new String(sb);
    }

    public static void closeDatabase() throws SQLException {
        stm.close();
        conn.close();
    }

    public DeThi(int soCau, String maMonThi, ArrayList<CauHoi> listCauHoi) {
        mSoCau = soCau;
        mMaMonThi = maMonThi;
        mListCauHoi = listCauHoi;
    }

    public int getSoCau() {
        return mSoCau;
    }

    public void setSoCau(int mSoCau) {
        this.mSoCau = mSoCau;
    }

    public String getMaMonThi() {
        return mMaMonThi;
    }

    public void setMaMonThi(String mMaMonThi) {
        this.mMaMonThi = mMaMonThi;
    }

    public ArrayList<CauHoi> getListCauHoi() {
        return mListCauHoi;
    }

    public void setListCauHoi(ArrayList<CauHoi> mListCauHoi) {
        this.mListCauHoi = mListCauHoi;
    }

    public static DeThi generateDeThi(String maMonThi, int soCau, ArrayList<CauHoi> boDe) {
        DeThi dt = new DeThi();
        dt.setSoCau(soCau);
        dt.setMaMonThi(maMonThi);
        ArrayList<CauHoi> listCauHoi = new ArrayList<CauHoi>();
        int i = 0;
        int soCauTrongBD = boDe.size();
        while (i < soCau) {
            int milisecond = Calendar.getInstance().get(Calendar.MILLISECOND);
            int thutuCauHoi = milisecond % soCauTrongBD;
            CauHoi ch = boDe.get(thutuCauHoi);
            if (!listCauHoi.contains(ch)) {
                listCauHoi.add(ch);
                i++;
            }
        }
        dt.setListCauHoi(listCauHoi);
        return dt;
    }

    public StringBuilder xemDeThi() {

        StringBuilder result = new StringBuilder();
        result.append("<table cellspacing=\"10\"><tbody>");
        for (int i = 0; i < mSoCau; i++) {
            CauHoi ch = mListCauHoi.get(i);
            result.append("<tr>");

            result.append("<td colspan=\"2\"><b>Cau ");
            result.append(i + 1);
            result.append(":  </b>");
            result.append(ch.getCauHoi());
            result.append("</td></tr>");

            for (CauTraLoi ctl : ch.getListCauTraLoi()) {
                result.append("<tr><td><input name=\"cau");
                result.append(i);
                result.append("\"  type=\"radio\" value=\"");
                result.append(ctl.getMaLuaChon());
                result.append("\"></td>");
                result.append("<td>");
                result.append(ctl.getMaLuaChon());
                result.append(". ");
                result.append(ctl.getLuaChon());
                result.append("</td></tr>");
            }
        }
        result.append("</tbody></table>");
        return result;
    }

    public String getDsCauHoi() {
        StringBuilder sb = new StringBuilder();
        for (CauHoi ch : mListCauHoi) {
            sb.append(ch.getCauHoi() + ";");
        }
        sb.deleteCharAt(sb.length() - 1);
        return new String(sb);
    }

    public int saveToDatabase(String masv, int lanThi) {
        try {
            openDatabase();
            // Sinh vien chua thi, chua co ket qua
            StringBuilder listMaCauHoi = new StringBuilder();
            for (CauHoi ch : mListCauHoi) {
                listMaCauHoi.append(ch.getMaCauHoi() + ";");
            }
            listMaCauHoi.deleteCharAt(listMaCauHoi.length() - 1);
            String s = new String(listMaCauHoi);
            String query = "insert into KetQuaThi values('" + masv + "', '" + mMaMonThi + "', " + lanThi + ", '" + s + "', '', '" + getDsDapAn() + "', -1)";
            if (stm.executeUpdate(query) == 1) {
                closeDatabase();
                return 1;
            }
            // Sinh vien da thi, nhan refresh.
            closeDatabase();
            return 403;
        } catch (SQLException ex) {
            try {
                closeDatabase();
            } catch (SQLException ex1) {
                Logger.getLogger(DeThi.class.getName()).log(Level.SEVERE, null, ex1);
            }
            return 403;
        }
    }
}
